package com.offcn.sellergood.controller;

import com.offcn.entity.PageResult;
import com.offcn.entity.Result;
import com.offcn.entity.StatusCode;
import com.offcn.sellergood.entity.GoodsEntity;
import com.offcn.sellergood.pojo.Goods;
import com.offcn.sellergood.service.GoodsService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.events.Event;

import java.sql.ResultSet;
import java.util.List;

/****
 * @Author:ujiuye
 * @Description:
 * @Date 2021/2/1 14:19
 *****/
@Api(tags = "GoodsController")
@RestController
@RequestMapping("/goods")
@CrossOrigin
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    // 添加商品GoodsEntity(tbGoods,tbGoodsDesc,itemList)
    @PostMapping
    public Result insert(@RequestBody GoodsEntity goodsEntity){
        try {
            goodsService.insert(goodsEntity);
            return new Result(true,StatusCode.OK,"添加商品复合实体类成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.OK,"添加商品复合实体类失败");
        }
    }

    // 根据Id获取商品信息
    @GetMapping("/{id}")
    public Result<GoodsEntity> findById(@PathVariable("id") Long id){
        try {
            GoodsEntity goodsEntity = goodsService.findById(id);
            return new Result<>(true,StatusCode.OK,"查询商品复合实体类成功",goodsEntity);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false,StatusCode.ERROR,e.getMessage(),null);
        }
    }

    // 商品审核通过自动上架
    @PutMapping("/audit/{id}")
    public Result audit(@PathVariable("id") Long goodsId){
        try {
            goodsService.audit(goodsId);
            return new Result(true,StatusCode.OK,"新款商品审核并上架成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,StatusCode.ERROR,e.getMessage());
        }
    }

    // 商品下架
    @PutMapping("/pull/{id}")
    public Result pull(@PathVariable("id") Long goodsId){
        try {
            goodsService.pull(goodsId);
            return new Result(true,StatusCode.OK,"新款商品下架成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,StatusCode.ERROR,e.getMessage());
        }
    }

    // 商品批量下架
    @PutMapping("/pull/all")
    public Result pullAll(@RequestBody Long[] goodsIds){
        try {
            int i = goodsService.pullAll(goodsIds);
            return new Result(true,StatusCode.OK,"下架" + i + "件商品");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.OK,"下架商品失败");
        }
    }

    // 商品上架
    @PutMapping("/push/{id}")
    public Result push(@PathVariable("id") Long goodsId){
        try {
            goodsService.push(goodsId);
            return new Result(true,StatusCode.OK,"新款商品上架成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,StatusCode.ERROR,e.getMessage());
        }
    }

    // 商品批量上架
    @PutMapping("/push/all")
    public Result pushAll(@RequestBody Long[] goodsIds){
        try {
            int i = goodsService.pushAll(goodsIds);
            return new Result(true,StatusCode.OK,"上架" + i + "件商品");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.OK,"上架商品失败");
        }
    }

    // 商品的逻辑删除
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Long id){
        try {
            goodsService.delete(id);
            return new Result(true,StatusCode.OK,"商品成功删除");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,e.getMessage());
        }
    }
}
